import {Injectable} from '@angular/core';

@Injectable()
export class Utility {
    public editideas:any = {};
    public checkEditUser:any;
    public UserPageInfo:any;
    public editUser:any = {};
    public userDetails:any;
    public postIdeaSaveData:any;
    public userDetailFlag:any;
    public postIdeaSaveDataStatus:boolean;
    public clickFromProfile:boolean = false;

    constructor() {
        this.editideas = {
            title: '',
            content: '',
            tags: ''
        };

        this.postIdeaSaveDataStatus = false;
    }

    public getFormattedDate(dates):any {
        let date = new Date(dates);
        let strTime:any;
        let minutes:any;
        let displayDate = (date.getUTCMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();
        let hours = date.getHours();
        minutes = date.getMinutes();
        let ampm = hours >= 12 ? 'PM' : 'AM';
        hours = hours % 12;
        hours = hours ? hours : 12; // the hour '0' should be '12'
        minutes = minutes < 10 ? '0' + minutes : minutes;
        strTime = displayDate + '  ' + hours + ':' + minutes + ' ' + ampm;
        return strTime;
    }

    public getAgoDate(dates):any {
        let date = new Date(dates);
        let dateToday = new Date();
        if (dateToday.getFullYear() === date.getFullYear()) {
            if ((dateToday.getUTCMonth() + 1) === (date.getUTCMonth() + 1)) {
                if ((dateToday.getDate() - date.getDate()) === 0) {
                    if ((dateToday.getHours() - date.getHours()) < 24) {
                        if ((dateToday.getHours() - date.getHours()) <= 1) {
                            if (dateToday.getMinutes() === date.getMinutes()) {
                                return 'now';
                            } else {
                                if ((dateToday.getHours() - date.getHours()) === 0) {
                                    return (dateToday.getMinutes() - (date.getMinutes()) + ' minutes ago');
                                }
                                else {
                                    if ((dateToday.getMinutes() + (60 - date.getMinutes())) > 60) {
                                        return '1 hour ago ';
                                    }
                                    else {
                                        return (dateToday.getMinutes() + (60 - date.getMinutes())) + ' minutes ago';
                                    }
                                }
                            }
                        }
                        else {
                            return (dateToday.getHours() - date.getHours()) + ' hours ago';
                        }
                    }
                    else {
                        return (dateToday.getHours() - date.getHours()) + ' hours ago';
                    }
                }
                else {
                    return this.getFormattedDate(dates);
                }
            }
            else {
                return this.getFormattedDate(dates);
            }
        }
        else {
            return this.getFormattedDate(dates);
        }
    }

    public getEditIdea(idea):any {
        this.editideas.id = idea._id;
        this.editideas.title = idea.title;
        this.editideas.content = idea.content;
        this.editideas.tags = idea.tags;
    }

    public getEditUser(user):any {
        this.editUser = user;
        this.checkEditUser = true;
    }

    public getUserProfileData(idea):any {
        this.UserPageInfo = idea;
    }

    public getXMonthsAgoDate(date, days):Date {
        // let  endDate =  new Date(date.getTime() - (days * 24 * 60 * 60 * 1000));
        let now = new Date();
        let sixMonthsFromNow = new Date(now.setMonth(now.getMonth() - 30));
        return sixMonthsFromNow;
    }

    public getMonthsAgoDate(months):Date {
        let now = new Date();
        let monthsFromNow = new Date(now.setMonth(now.getMonth() - months));
      console.log('************monthsFromNow******************', monthsFromNow);
      return monthsFromNow;
    }

    public getDatewithoutTimstamp(date):string {
        return date.getUTCMonth() + 1 + '/' + date.getDate() + '/' + date.getFullYear();
    }

    public isDateValid(inputDate:string):boolean {
        let valid = true;
        let date = inputDate.split('/');
        if (date[0] === 'mm' || date[1] === 'dd' || date[2] === 'yyyy' || !date) valid = false;
        let month = parseInt(date[0]);
        let day = parseInt(date[1]);
        let year = parseInt(date[2]);
        if ((month < 1) || (month > 12)) valid = false;
        else if ((day < 1) || (day > 31)) valid = false;
        else if (((month === 4) || (month === 6) || (month === 9) || (month === 11)) && (day > 30)) valid = false;
        else if ((month === 2) && (((year % 400) === 0) || ((year % 4) === 0)) && ((year % 100) != 0) && (day > 29)) valid = false;
        else if ((month === 2) && ((year % 100) === 0) && (day > 29)) valid = false;
        else if ((month === 2) && (day > 28)) valid = false;
        let selectedDate = new Date(inputDate);
        if (selectedDate.getTime() > new Date().getTime()) {
            valid = false;
        }
        return valid;
    }

    public checkFromToDateValiDity(fromDate, toDate):boolean {
        let valid = false;
        if (this.isDateValid(fromDate) && this.isDateValid(toDate)) {
            if (new Date(fromDate).getTime() <= new Date(toDate).getTime()) {
                valid = true;
            }
        }
        return valid;
    }


    public getFormattedDateForTree(startDate) {
        let date = new Date(startDate);
        let day1:any;
        let month1:any;
        day1 = date.getDate();
        month1 = date.getMonth() + 1;
        let year1 = date.getFullYear();

        if (month1 < 10) {
            month1 = 0 + '' + month1;
        }
        if (day1 < 10) {
            day1 = 0 + '' + day1;
        }

        let date1 = day1 + "/" + month1 + "/" + year1;
        return date1;
    }

    // onclick show user log page
    public getUserInfo(user):any {
        this.userDetails = user;
        this.userDetailFlag = true;
    }

    public savePostIdea(input) {
        this.postIdeaSaveData = input;
    }
}

